//
//  ZTAirChartView.m
//  YSSmartHome
//
//  Created by yanghao on 2017/4/6.
//  Copyright © 2017年 yahaong. All rights reserved.
//

#import "ZTAirChartView.h"

#import "YSCommon.h"
#import "CommonUtils.h"

@interface ZTAirChartView ()
@property (nullable, nonatomic, strong) NSArray* lables;
@property (nullable, nonatomic, strong) UILabel* mlblTitle;
@property (nullable, nonatomic, strong) UIImageView* mImgTopBg;
@property (nullable, nonatomic, strong) PNLineChart* charlineView;

@end
@implementation ZTAirChartView

- (instancetype _Nullable )initWithXlabels:(NSArray*__nonnull)labes block:(YLabelBlockFormatter _Nullable )yLabelBlockFormatter;
{
    self = [super init];
    if (self) {
        _lables = labes;
        self.yLabelBlockFormatter = yLabelBlockFormatter;
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews
{
    [self addSubview:self.mImgTopBg];
    [self.mImgTopBg addSubview:self.mlblTitle];
    [self.mImgTopBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self);
        make.height.equalTo(kIphoneSize_Height(20));
    }];
    [self.mlblTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.mImgTopBg);
    }];
    [self addSubview:self.charlineView];
    self.layer.cornerRadius = 5;
    self.layer.masksToBounds = YES;
}


- (BOOL)isSameDay:(NSInteger )time1 date2:(NSInteger)time2
{
    NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:time1];
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:time2];
    NSCalendar* calendar = [NSCalendar currentCalendar];
    unsigned unitFlags =  NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;
    NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1];
    NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2];
    return [comp1 day]   == [comp2 day] &&
    [comp1 month] == [comp2 month] &&
    [comp1 year]  == [comp2 year];
}




- (void)setXlables:(NSArray *)xlables
{

    
    NSMutableArray* arrayM = [NSMutableArray array];
    for (AirHistoryModel *modle in xlables) {
        if ([self isSameDay:modle.time date2:[[NSDate date] timeIntervalSince1970]]) {
            if (![arrayM containsObject:modle] ) {
               [arrayM addObject:modle];
            }
        }
    }
    NSMutableArray* periodAllArray = [NSMutableArray array];
    NSMutableArray* period03Array   = [NSMutableArray array];
    NSMutableArray* period06Array   = [NSMutableArray array];
    NSMutableArray* period09Array   = [NSMutableArray array];
    NSMutableArray* period12Array   = [NSMutableArray array];
    NSMutableArray* period15Array   = [NSMutableArray array];
    NSMutableArray* period18Array   = [NSMutableArray array];
    NSMutableArray* period21Array   = [NSMutableArray array];
    NSMutableArray* period24Array   = [NSMutableArray array];
    for (AirHistoryModel *model in arrayM) {
        
        NSDate* date = [NSDate dateWithTimeIntervalSince1970:model.time];
        if ([CommonUtils dateIsBetweenPeriodOfDate:date datePeriod:DatePeriod03]) {
            [period03Array addObject:model];
        }
        else if ([CommonUtils dateIsBetweenPeriodOfDate:date datePeriod:DatePeriod06]) {
            [period06Array addObject:model];
        }
        else if ([CommonUtils dateIsBetweenPeriodOfDate:date datePeriod:DatePeriod09]) {
            [period09Array addObject:model];
        }

        else if ([CommonUtils dateIsBetweenPeriodOfDate:date datePeriod:DatePeriod12]) {
            [period12Array addObject:model];
        }

        else if ([CommonUtils dateIsBetweenPeriodOfDate:date datePeriod:DatePeriod15]) {
            [period15Array addObject:model];
        }

        else if ([CommonUtils dateIsBetweenPeriodOfDate:date datePeriod:DatePeriod18]) {
            [period18Array addObject:model];
        }

        else if ([CommonUtils dateIsBetweenPeriodOfDate:date datePeriod:DatePeriod21]) {
            [period21Array addObject:model];
        }
        else if ([CommonUtils dateIsBetweenPeriodOfDate:date datePeriod:DatePeriod24]) {
            [period24Array addObject:model];
        }

    }
    
    

    
    NSNumber* avgNum = [self avgFromArray:period03Array];
    if (avgNum) {
        [periodAllArray addObject:avgNum];
    }
    else
    {
        [periodAllArray addObject:@(0)];
    }
    
    avgNum = [self avgFromArray:period06Array];
    if (avgNum) {
        [periodAllArray addObject:avgNum];
    }
    else
    {
        [periodAllArray addObject:@(0)];
    }

    avgNum = [self avgFromArray:period09Array];
    if (avgNum) {
        [periodAllArray addObject:avgNum];
    }
    else
    {
        [periodAllArray addObject:@(0)];
    }

    avgNum = [self avgFromArray:period12Array];
    if (avgNum) {
        [periodAllArray addObject:avgNum];
    }
    else
    {
        [periodAllArray addObject:@(0)];
    }

    avgNum = [self avgFromArray:period15Array];
    if (avgNum) {
        [periodAllArray addObject:avgNum];
    }
    else
    {
        [periodAllArray addObject:@(0)];
    }

    avgNum = [self avgFromArray:period18Array];
    if (avgNum) {
        [periodAllArray addObject:avgNum];
    }
    else
    {
        [periodAllArray addObject:@(0)];
    }

    avgNum = [self avgFromArray:period21Array];
    if (avgNum) {
        [periodAllArray addObject:avgNum];
    }
    else
    {
        [periodAllArray addObject:@(0)];
    }

    avgNum = [self avgFromArray:period24Array];
    if (avgNum) {
        [periodAllArray addObject:avgNum];
    }
    else
    {
        [periodAllArray addObject:@(0)];
    }


//    [_charlineView setXLabels:periodAllArray];
    
    
    PNLineChartData *data01 = [PNLineChartData new];
    data01.color = PNFreshGreen;
    data01.inflexionPointStyle = PNLineChartPointStyleCircle;
    data01.itemCount = periodAllArray.count;
    data01.getData = ^(NSUInteger index) {
        CGFloat yValue = [periodAllArray[index] floatValue];
        return [PNLineChartDataItem dataItemWithY:yValue];
    };
    
    [self.charlineView updateChartData:@[data01]];
}

- (NSNumber*)avgFromArray:(NSArray*)array
{
    if (array.count > 0) {
        NSArray* dataArray = [array valueForKey:@"value"] ;
        NSNumber* avg =  [dataArray valueForKeyPath:@"@avg.self"];
        return avg;
    }
    return NULL;
    
}
- (NSString* )dateTimeToString:(uint32_t)time
{
    NSDate *date =  [NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/BeiJing"];
    [dateFormatter setTimeZone:timeZone];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *dateStr = [dateFormatter stringFromDate:date];
    return dateStr;

}

#pragma mark - getters and setters 
- (void)setTopBackgroundImage:(UIImage *)topBackgroundImage
{
    _topBackgroundImage = topBackgroundImage;
    self.mImgTopBg.image = topBackgroundImage;
}
- (void)setTitle:(NSString *)title
{
    _title = [title copy];
    self.mlblTitle.text = title;
}
- (UILabel *)mlblTitle
{
    if (!_mlblTitle) {
        _mlblTitle = [[UILabel alloc]init];
        _mlblTitle.font = [UIFont systemFontOfSize:13];
    }
    return _mlblTitle;
}
- (UIImageView *)mImgTopBg
{
    if (!_mImgTopBg) {
        _mImgTopBg = [[UIImageView alloc]init];
    }
    return _mImgTopBg;
}
- (PNLineChart *)charlineView
{
    if (!_charlineView) {
        _charlineView = [[PNLineChart alloc] initWithFrame:CGRectMake(0, kIphoneSize_Height(20), SCREEN_WIDTH - kIphoneSize_Widith(20), 150)];
        _charlineView.showSmoothLines = YES;
        _charlineView.showCoordinateAxis = YES;
        _charlineView.showYGridLines = YES;
        _charlineView.xLabelFont = [UIFont fontWithName:@"Helvetica-Light" size:8.0];
        [_charlineView setXLabels:self.lables];
        if (self.yLabelBlockFormatter ) {
            _charlineView.yLabelBlockFormatter = self.yLabelBlockFormatter;
        }
        // Line Chart No.1
        NSArray * data01Array = @[@60.1, @160.1, @126.4, @262.2, @186.2];
        PNLineChartData *data01 = [PNLineChartData new];
        data01.color = PNFreshGreen;
        data01.inflexionPointStyle = PNLineChartPointStyleCircle;
        data01.itemCount = 5;
        data01.getData = ^(NSUInteger index) {
            CGFloat yValue = [data01Array[index] floatValue];
            return [PNLineChartDataItem dataItemWithY:yValue];
        };
        _charlineView.chartData = @[data01];
        [_charlineView strokeChart];
    }
    return _charlineView;
}
@end
